class PurchasesController < ApplicationController
  
  def show_address
    @delivery_address = Address.new
  end
  
  def set_address
    session['delivery_address'] = params[:address]
    @payment_methods = PaymentMethod.all
    render :action => 'show_payment'
  end
  
  def show_payment
  
  end
  
  def set_payment
    session['payment_method_id'] = params[:payment_method_id]
    payment_method = PaymentMethod.find(params[:payment_method_id])
    delivery_address = Address.new(session['delivery_address'])
    status_new = Status.find_by_name('Novo')
    @purchase = Purchase.new(:payment_method => payment_method, :delivery_address => delivery_address, :status => status_new)
    total_value = 0
    session['cart'].each do |cart_item|
      @purchase.purchase_items << PurchaseItem.new(:product_id => cart_item.product_id, :quantity => cart_item.quantity)
      unless cart_item.price.nil?
        total_value += cart_item.quantity * cart_item.price
      end
    end
    session['purchase'] = @purchase
    #validar a compra
    session['payment_error'] = nil
    if params[:payment_method_id] == '10'   # Cartao
      begin
        credit_card_result = PaymentInfo.new.validate_purchase_credit_card(total_value, params[:card_owner_name], "VISA", params[:card_number], params[:card_expiration], params[:card_security_code], 1)
      rescue StandardError => e
        status_payment_error = Status.find_by_name('Problema no Pagamento')
        @purchase.status = status_payment_error
        session['payment_error'] = e.message
      end
      if credit_card_result == "true"
        status_approved = Status.find_by_name('Pagamento Aprovado')
        @purchase.status = status_approved
      else
        status_payment_error = Status.find_by_name('Problema no Pagamento')
        @purchase.status = status_payment_error
        session['payment_error'] = "Saldo insuficiente"
      end
    elsif params[:payment_method_id] == '9'   # Boleto
      begin
        billet_result = PaymentBilletInfo.new.validate_purchase_billet('12', '74774701', total_value)
      rescue StandardError => e
        status_payment_error = Status.find_by_name('Problema no Pagamento')
        @purchase.status = status_payment_error
        session['payment_error'] = e.message
      end
      unless billet_result.nil?
        status_waiting = Status.find_by_name('Aguardando Pagamento')
        @purchase.status = status_waiting
      end
    end
    render :action => 'purchase_confirmation'
  end
  
  def purchase_confirmation
    
  end
  
  def end_purchase
    purchase = session['purchase']
    @purchase = Purchase.new(:payment_method => purchase.payment_method, :delivery_address => purchase.delivery_address, :status => purchase.status, :purchase_items => purchase.purchase_items)
    @purchase.save
#    credit_situation = UserInfo.new.check_credit_situation(session['user_cpf'])
#    if credit_situation == 'irregular'
#      status_credit_not_approved = Status.find_by_name('Nao aprovado')
#      @purchase.update_attribute(:status, status_credit_not_approved)
#    end
  end
  
  # GET /purchases
  # GET /purchases.json
  def index
    @purchases = Purchase.all
  end

  # GET /purchases/1
  # GET /purchases/1.json
  def show
    @purchase = Purchase.find(params[:id])
  end

  # POST /purchases
  # POST /purchases.json
  def create
    @purchase = Purchase.new(params[:purchase])
    flash[:notice] = 'Pedido criado com sucesso.' if @purchase.save
  end

end
